package itsm.isperp.module.service.wechat;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.entity.wechat.WechatNewsArticle;
import itsm.isperp.module.repository.wechat.WechatNewsArticleMapper;
import itsm.isperp.module.service.app.AppDictService;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * 服务
 * 
 * @author lizx
 * @date 2014-11-05 23:56
 */
@Service
public class WechatNewsArticleService extends BaseService<WechatNewsArticle> {

	@Autowired
	protected WechatNewsArticleMapper wechatNewsArticleMapper;

	@Override
	public WechatNewsArticleMapper getRepository() {
		return this.wechatNewsArticleMapper;
	}

	public List<WechatNewsArticle> findByNewsId(String newsId,String openId) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.setSort("order_num", Direction.ASC);
		req.addFilterRule("news_id", JqGridFilterRuleOp.eq, newsId);
		
		AppDictService appDictService=ContextHolder.getSpringBean("appDictService");
		List<AppDict> list=appDictService.findByType("WECHAT_MENU_CONTROL");
		if(StringUtils.isNotBlank(openId) && list.size()>0)
		{
		  String description=list.get(0).getDescription();
		  description=description.replace("{openId}", openId);
		  if(StringUtils.isNotBlank(description))
		  {
		    req.setSearchString(description);
		  }
		  
		}
		
		return this.findAll(req);

	}

}
